package com.sitanyun.merchant.guide.view.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.StringUtils;
import com.sitanyun.merchant.guide.R;
import com.sitanyun.merchant.guide.base.BaseActivity;
import com.sitanyun.merchant.guide.base.ToastUtil;
import com.sitanyun.merchant.guide.base.utils.NetworkStateUtils;
import com.sitanyun.merchant.guide.bean.ForgetBean;
import com.sitanyun.merchant.guide.bean.ForgetsmscoedeBean;
import com.sitanyun.merchant.guide.bean.SmsPhoneBean;
import com.sitanyun.merchant.guide.presenter.impl.ForgetPasswordAPresenterImpl;
import com.sitanyun.merchant.guide.presenter.inter.IForgetPasswordAPresenter;
import com.sitanyun.merchant.guide.view.inter.IForgetPasswordAView;
import com.sitanyun.merchant.guide.weiht.CodeEditView;
import com.sitanyun.merchant.guide.weiht.CountDownTimers;
import com.sitanyun.merchant.guide.weiht.DateUtils;
import com.sitanyun.merchant.guide.weiht.PhoneUtils;
import com.sitanyun.merchant.guide.weiht.SharedPreferenceUtil;
import com.sitanyun.merchant.guide.weiht.StepViews;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 忘记密码
 */
public class ForgetPasswordActivity extends BaseActivity implements IForgetPasswordAView {

//    @BindView(R.id.sv_foget)
//    StepView svFoget;
    @BindView(R.id.et_forget_phone)
    EditText etForgetPhone;
    @BindView(R.id.phone_message)
    TextView phoneMessage;
    @BindView(R.id.phone_next)
    TextView phoneNext;
    @BindView(R.id.ll_forget_one)
    LinearLayout llForgetOne;
    @BindView(R.id.codeEditView)
    CodeEditView codeEditView;

    @BindView(R.id.phone_next_two)
    TextView phoneNextTwo;
    @BindView(R.id.ll_forget_two)
    LinearLayout llForgetTwo;
    @BindView(R.id.et_fogethree_phone)
    EditText etFogethreePhone;
    @BindView(R.id.foget_phone_msgthree)
    TextView fogetPhoneMsgthree;
    @BindView(R.id.et_fogethree_password)
    EditText etFogethreePassword;
    @BindView(R.id.paswd_layout)
    LinearLayout paswdLayout;
    @BindView(R.id.foget_pass_msgthree)
    TextView fogetPassMsgthree;
    @BindView(R.id.phone_next_three)
    TextView phoneNextThree;
    @BindView(R.id.ll_forget_three)
    LinearLayout llForgetThree;
    @BindView(R.id.tv_foget_last)
    TextView tvFogetLast;
    @BindView(R.id.ll_foget_for)
    LinearLayout llFogetFor;
    @BindView(R.id.message_two)
    TextView messageTwo;
    @BindView(R.id.forget_sms_msg)
    TextView forgetSmsMsg;
    @BindView(R.id.cxhq)
    TextView cxhq;
    @BindView(R.id.showdown)
    TextView showdown;
    @BindView(R.id.sv_step)
    StepViews svStep;
    private IForgetPasswordAPresenter mIForgetPasswordAPresenter;
    private String codes;
    private int recLen = 6;//跳过倒计时提示6秒
    Timer timer = new Timer();

    @Override
    public int getLayoutId() {
        return R.layout.activity_forget_password;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        setFindViewById(true);
        setTitleStr("验证手机号码");
        //自动弹出键盘问题解决
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }

    }

    @Override
    public void initView() {

        svStep.setProgress(1, 3);
        List<String> list = new ArrayList<>();
        list.add("验证手机");
        list.add("设置密码");
        list.add("重置密码");
        svStep.setTitles(list);

        codeEditView.clearText();
        //监听
        codeEditView.setOnCompleteListener(new CodeEditView.OnCompleteListener() {
            @Override
            public void onComplete(String content) {
                codes = content;
            }
        });

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mIForgetPasswordAPresenter = new ForgetPasswordAPresenterImpl(this);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    @OnClick({R.id.phone_next, R.id.phone_next_two, R.id.phone_next_three, R.id.tv_foget_last})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            //第一步下一步
            case R.id.phone_next:
                if (DateUtils.isFastClick()) {
                    return ;
                }
                if (!NetworkStateUtils.isNetworkConnected(this)) {
                    ToastUtil.showToast(this, "网络异常,请稍后重试");
                    // this.finish();
                    return;
                }
                String phone = etForgetPhone.getText().toString().trim();
                if (phone.isEmpty()) {
                    ToastUtil.showToast(ForgetPasswordActivity.this, "请输入手机号码");
                    return;
                } else if (!PhoneUtils.isMobleNo(phone)) {
                    Toast.makeText(this, "手机号格式有误", Toast.LENGTH_SHORT).show();
                    return;
                }
                mIForgetPasswordAPresenter.getLoginphone(etForgetPhone.getText().toString().trim());
                break;
            //第二页下一步
            case R.id.phone_next_two:
                if (DateUtils.isFastClick()) {
                    return ;
                }
                mIForgetPasswordAPresenter.getsmscode(etForgetPhone.getText().toString().trim(), codes, "3");
                break;
            //第三页下一步
            case R.id.phone_next_three:
                if (DateUtils.isFastClick()) {
                    return ;
                }
                String threephone = etFogethreePhone.getText().toString().trim();
                String threepaswd = etFogethreePassword.getText().toString().trim();
                if (threephone.isEmpty()) {
                    fogetPhoneMsgthree.setText("请输入密码");
                    fogetPhoneMsgthree.setVisibility(View.VISIBLE);
                    return;
                }
                if (threepaswd.isEmpty()) {
                    fogetPassMsgthree.setText("请再次输入密码");
                    fogetPhoneMsgthree.setVisibility(View.GONE);
                    fogetPassMsgthree.setVisibility(View.VISIBLE);
                    return;
                }
                if (!StringUtils.equals(threephone, threepaswd)) {
                    fogetPassMsgthree.setText("两次密码输入不一致");
                    fogetPhoneMsgthree.setVisibility(View.GONE);
                    fogetPassMsgthree.setVisibility(View.VISIBLE);
                    return;
                }

                if (threephone.length() < 6) {
                    fogetPhoneMsgthree.setText("格式有误，请输入6-12位数字字母组合密码");
                    fogetPhoneMsgthree.setVisibility(View.VISIBLE);
                    fogetPassMsgthree.setVisibility(View.GONE);
                    return;
                }
                if (threepaswd.length() < 6) {
                    fogetPassMsgthree.setText("格式有误，请输入6-12位数字字母组合密码");
                    fogetPhoneMsgthree.setVisibility(View.GONE);
                    fogetPassMsgthree.setVisibility(View.VISIBLE);
                    return;
                }
                if (!PhoneUtils.isLetterDigit(threephone) || !PhoneUtils.isLetterDigit(threepaswd)) {
                    fogetPhoneMsgthree.setText("格式有误，请输入6-12位数字字母组合密码");
                    fogetPhoneMsgthree.setVisibility(View.VISIBLE);
                    fogetPassMsgthree.setVisibility(View.GONE);
                    return;
                }

                mIForgetPasswordAPresenter.getpaswd(etForgetPhone.getText().toString().trim(), codes, threepaswd);

                break;
            //成功返回
            case R.id.tv_foget_last:
                if (DateUtils.isFastClick()) {
                    return ;
                }timer.cancel();
                SharedPreferenceUtil.clearAll();
                Intent intent = new Intent(ForgetPasswordActivity.this, LoginActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                finish();

                break;
        }
    }

    @Override
    public <T> T request(int requestFlag) {
        return null;
    }

    @Override
    public <T> void response(T response, int responseFlag) {
        if (responseFlag == IForgetPasswordAView.RESPONSE_ONE) {
            SmsPhoneBean smsPhoneBean = (SmsPhoneBean) response;
            if (smsPhoneBean.getCode() == 0) {
                phoneMessage.setVisibility(View.GONE);
                 //第二页显示
                llForgetOne.setVisibility(View.GONE);
                llForgetTwo.setVisibility(View.VISIBLE);
                initsms();
            } else {
                phoneMessage.setVisibility(View.VISIBLE);
                phoneMessage.setText(smsPhoneBean.getMsg().toString());
            }

        }

    }

    //获取验证码
    @Override
    public <T> void getresponsesms(T response, int responseFlag) {
        if (responseFlag == IForgetPasswordAView.RESPONSE_ONE) {
            SmsPhoneBean smsPhoneBean = (SmsPhoneBean) response;
            if (smsPhoneBean.getCode() == 0) {
                 ToastUtil.showToast(ForgetPasswordActivity.this, "验证码发送成功");
            } else {
                ToastUtil.showToast(ForgetPasswordActivity.this, smsPhoneBean.getMsg() + "");
            }

        }
    }

    //验证码接口验证
    @Override
    public <T> void responsesms(T response, int responseFlag) {
        if (responseFlag == IForgetPasswordAView.RESPONSE_ONE) {
            ForgetsmscoedeBean forgetsmscoedeBean = (ForgetsmscoedeBean) response;
            if (forgetsmscoedeBean.getCode() == 0) {
                llForgetTwo.setVisibility(View.GONE);
                llForgetThree.setVisibility(View.VISIBLE);
                setTitleStr("设置密码");
                svStep.setProgress(2, 3);
//                int nextStep1 = svFoget.getCurrentStep() + 1;
//                svFoget.selectedStep(nextStep1);
            } else {
                codeEditView.clearText();
                forgetSmsMsg.setText(forgetsmscoedeBean.getMsg() + "");
                forgetSmsMsg.setVisibility(View.VISIBLE);
            }

        }
    }

    //验证密码
    @Override
    public <T> void yzpassword(T response, int responseFlag) {
        if (responseFlag == IForgetPasswordAView.RESPONSE_ONE) {
            ForgetBean forgetBean = (ForgetBean) response;
            if (forgetBean.getCode() == 0) {
                //第四页
                forgetSmsMsg.setVisibility(View.GONE);
                llForgetThree.setVisibility(View.GONE);
                llFogetFor.setVisibility(View.VISIBLE);
                svStep.setProgress(3, 3);
//                int nextStep1 = svFoget.getCurrentStep() + 2;
//                svFoget.selectedStep(nextStep1);
                initfor();

            } else {
//                forgetSmsMsg.setText(forgetBean.getMsg() + "");
//                forgetSmsMsg.setVisibility(View.VISIBLE);
                fogetPhoneMsgthree.setText(forgetBean.getMsg() + "");
                fogetPhoneMsgthree.setVisibility(View.VISIBLE);

            }

        }
    }

    //第四页
    private void initfor() {
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        recLen--;
                        showdown.setText(recLen + "秒后进入登录界面");
                        if (recLen == 0) {
                            timer.cancel();
                            showdown.setVisibility(View.GONE);//倒计时到0隐藏字体
                            SharedPreferenceUtil.clearAll();
                            Intent intent = new Intent(ForgetPasswordActivity.this, LoginActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                             finish();
                        }
                    }
                });

            }
        };

        timer.schedule(timerTask, 1000, 1000);//等待时间一秒，停顿时间一秒
    }


    //验证码验证
    private void initsms() {
        CountDownTimers mCountDownTimerUtils = new CountDownTimers(cxhq, 60000, 1000);
        mCountDownTimerUtils.start();
        messageTwo.setText("我们已经向" + etForgetPhone.getText().toString().trim() + "发送验证码");
        mIForgetPasswordAPresenter.getsmslogin(etForgetPhone.getText().toString().trim(), "3");
    }

    @OnClick(R.id.cxhq)
    public void onViewClicked() {
        if (cxhq.getText().equals("重新获取验证码")) {
            mIForgetPasswordAPresenter.getsmslogin(etForgetPhone.getText().toString().trim(), "3");
            CountDownTimers mCountDownTimerUtils = new CountDownTimers(cxhq, 60000, 1000);
            mCountDownTimerUtils.start();

        }
    }
}
